//
//  UIFactory.m
//  zxcrm
//
//  Created by 刘佳乐 on 2020/5/15.
//  Copyright © 2020 user. All rights reserved.
//

#import "UIFactory.h"

@implementation UIFactory


+(UIFactory *)shareInstace{
    static UIFactory *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[UIFactory alloc] init];
    });
    return share;
}

-(UIButton *)setButton:(UIButtonType)type frame:(CGRect)frame image:(nullable UIImage *)image title:(NSString *)title font:( nullable UIFont *)font backColot:(UIColor*)backColor textColor:(UIColor *)textColor target:(nullable id)target action:(SEL)action layer:(float)layer{
    
    
    UIButton *button = [UIButton buttonWithType:type];
    button.frame = frame;
    button.backgroundColor = backColor;
    button.titleLabel.font = font;
    [button setTitleColor:textColor forState:UIControlStateNormal];
    if(title){
        [button setTitle:title forState:UIControlStateNormal];
    }
    
    if(image){
        [button setImage:image forState:UIControlStateNormal];
    }
    if(target){
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    
    if(layer > 0){
        button.layer.cornerRadius = layer;
        button.layer.masksToBounds = YES;
    }
    
    
    return button;
}

-(UILabel *)setLabel:(CGRect)frame text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = font;
    label.textColor = textColor;
    return label;
}

-(CGSize)getStrRect:(UIFont *)font str:(NSString *)str height:(double)height width:(double)width{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    CGSize size = CGSizeZero;
    size = height == 0 ? CGSizeMake(width, MAXFLOAT) : CGSizeMake(MAXFLOAT, height);
    return [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs
                             context:nil].size;
    
}

-(UIView *)setLineView:(CGRect)frame color:(UIColor *)color{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = color;
    return view;
}


- (NSString *)deleteStr:(NSString *)str repalcesStr:(NSString *)repalcesStr repalcesStr1:(NSString *)repalcesStr1{
    if([str containsString:repalcesStr]){
        NSRange range = [str rangeOfString:repalcesStr];
        str =  [str stringByReplacingCharactersInRange:range withString:repalcesStr1];
        return [self deleteStr:str repalcesStr:repalcesStr repalcesStr1:repalcesStr1];
    }else{
        return str;
    }
    
}

-(NSMutableAttributedString *)setPlaceHolder:(UIFont *)font color:(UIColor *)color placeHolder:(nonnull NSString *)placeHolder{
    return [[NSMutableAttributedString alloc] initWithString:placeHolder attributes:@{NSForegroundColorAttributeName : color , NSFontAttributeName:font}];;
}

//获取view的controller
- (UIViewController *)viewController:(UIView*)view;
{
    for (UIView* next = [view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

//左右渐变
- (void)addTransitionColorLeftToRight:(UIColor *)startColor endColor:(UIColor *)endColor view:(UIView *)view {
    [self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0) view:view];
}

//斜渐变
- (void)addTransitionColor:(UIColor *)startColor endColor:(UIColor *)endColor  view:(UIView *)view{
    [self addTransitionColor:startColor endColor:endColor startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1) view:view];
}

- (void)addTransitionColor:(UIColor *)startColor
                  endColor:(UIColor *)endColor
                startPoint:(CGPoint)startPoint
                  endPoint:(CGPoint)endPoint
                      view:(UIView *)view{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = @[@0, @1];
    gradientLayer.startPoint = startPoint;
    gradientLayer.endPoint = endPoint;
    gradientLayer.frame = view.bounds;
    [view.layer insertSublayer:gradientLayer atIndex:0];
}

-(void)showAlert:(NSString*)confirmStr cancelStr:(NSString*)cancelStr title:(NSString*)title message:(NSString*)message viewController:(UIViewController *)viewController
           confirmAction:(void (^ __nullable)(UIAlertAction *action))confirmAction cancelAction:(void (^ __nullable)(UIAlertAction *action))cancelAction {
    //显示提示框
    //过时
    //    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
    //    [alert show];
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* confirm = [UIAlertAction actionWithTitle:confirmStr style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * action) {
        //响应事件
        confirmAction(action);
        
    }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:cancelStr style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
        //响应事件
        cancelAction(action);
        
    }];
    
    
    [alert addAction:cancel];
    [alert addAction:confirm];
    [[self getCurrentVC] presentViewController:alert animated:YES completion:nil];
}


- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}




@end
